package com.jingdianjichi.sunject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.sunject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.sunject.application.dto.SubjectCategoryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题模块
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 增加分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.converDtoToBo(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    /**
     * 查询分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){
            log.info("queryPrimary--CategoryController:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE
                    .converDtoToBo(subjectCategoryDTO);

            List<SubjectCategoryBo> subjectCategoryBoList=subjectCategoryDomainService.queryCategory(subjectCategoryBo);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter
                    .INSTANCE.converBoListToDtoList(subjectCategoryBoList);

            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询分类下的小分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){
            log.info("queryCategoryByPrimary--CategoryController:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"父ID不能为空");
        Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE
                    .converDtoToBo(subjectCategoryDTO);

            List<SubjectCategoryBo> subjectCategoryBoList=subjectCategoryDomainService.queryCategory(subjectCategoryBo);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter
                    .INSTANCE.converBoListToDtoList(subjectCategoryBoList);

            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新分类
     * @return
     */
    @PostMapping("/update")
    public Result updateCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if (log.isInfoEnabled()){
            log.info("updateCategory---Controller:{}",JSON.toJSONString(subjectCategoryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"ID不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE
                    .converDtoToBo(subjectCategoryDTO);
            subjectCategoryDomainService.updateCategory(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("Message---updateCategory---Controller:{}",e.getMessage());
            return Result.fail("更新失败");
        }
    }
    /**
     * 删除分类
     */

    @PostMapping("/delete")
    public Result deleteCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if (log.isInfoEnabled()){
            log.info("deleteCategory---Controller:{}",JSON.toJSONString(subjectCategoryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"ID不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE
                    .converDtoToBo(subjectCategoryDTO);

            subjectCategoryDomainService.deleteCategory(subjectCategoryBo);
            return Result.ok();
        } catch (Exception e) {
            log.info("Message---deleteCategory---Controller:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }


}
